package com.bgs.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bgs.entity.*;
import com.bgs.entity.Collections;
import com.bgs.mapper.LjpexclusiveMapper;
import com.bgs.pojo.*;
import com.bgs.service.LjpexclusiveService;
import com.bgs.tool.AliOss;
import com.bgs.tool.R;
import com.bgs.tool.RedisUtil;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Service
@Transactional
public class LjpexclusiveServiceImpl implements LjpexclusiveService {

    @Autowired
    LjpexclusiveMapper ljpexclusiveMapper;

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    AliOss aliOss;

    @Override
    public R getunion(String keyword,Integer userid) {
        String content="%"+keyword+"%";
        //查询垃圾分类表模糊查信息
        List<Classification> c=ljpexclusiveMapper.getclassificationlike(content);
        //查询商品表模糊查信息
        List<CommodityUnion> commodities=ljpexclusiveMapper.getcommoditylike(content);
        //查询讨论表模糊查信息
        List<Discuss> discusses=ljpexclusiveMapper.getdiscusslike(content);
        ConcurrentMap map=new ConcurrentHashMap();
        map.put("c",c);
        map.put("commodities",commodities);
        map.put("discusses",discusses);
        if(redisUtil.get("search-history-"+userid)==null){
            List<Searchhistory> list=new ArrayList<>();
            Searchhistory searchhistory=new Searchhistory();
            searchhistory.setName(keyword);
            list.add(searchhistory);

            redisUtil.set("search-history-"+userid,list);
        }else {
            List<Searchhistory> list=(List<Searchhistory>) redisUtil.get("search-history-" + userid);
            Searchhistory searchhistory=new Searchhistory();
            searchhistory.setName(keyword);
            int count=0;
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).getName());
                if (list.get(i).getName().equals(keyword)){
                    count++;
                }
                if (i>5){
                    break;
                }
            }
            System.out.println("count:"+count);
            if(count ==0){
                list.add(0,searchhistory);
            }

            redisUtil.set("search-history-"+userid,list);
        }
        return R.ok().data("unionmap",map);
    }

    @Override
    public R getsearchhistory(Integer userid) {
        List<Discuss> discusses=ljpexclusiveMapper.getrm();
        ConcurrentMap map=new ConcurrentHashMap();
        map.put("discusses",discusses);
//        List<String[]> strings = JSONObject.parseArray((String) redisUtil.get("search-history-" + userid), String[].class);
        User user=(User) redisUtil.get("usersss");
//        String[] ss=(String[]) redisUtil.get("search-history-" + userid);replace
        List<Searchhistory> list=(List<Searchhistory>) redisUtil.get("search-history-" + userid);
        if(list.size()<5 || list.size()==5){
            map.put("ss",list);
            return R.ok().data("map",map);
        }else {
            List<Searchhistory> list1=new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                Searchhistory searchhistory=new Searchhistory();
                searchhistory.setName(list.get(i).getName());
                 list1.add(searchhistory);
            }
            map.put("ss",list1);
            return R.ok().data("map",map);
        }

    }

    @Override
    public R dells(Integer userid) {
        redisUtil.del("search-history-"+userid);
        return R.ok();
    }

    @Override
    public R getclassxq(Integer id) {
        Classification c=ljpexclusiveMapper.getclassxq(id);
        Classification c1=ljpexclusiveMapper.getclassxq1(c.getFatherId());
        List<Classification> c3=ljpexclusiveMapper.getclassxq2(id);
        c.setClassification(c1);
        c.setClassificationList(c3);

        return R.ok().data("c",c);
    }

    @Override
    public R inittlxq(Integer id) {
        System.out.println("讨论id："+id);
        DiscussUnion discuss=ljpexclusiveMapper.getdiscussdan(id);
        List<Comment> list=ljpexclusiveMapper.getcommentbytlid(id);
        //获取讨论获赞数
        Integer follow=(Integer) redisUtil.get("follow"+id);
        Integer comment=(Integer) redisUtil.get("comment"+id);
        discuss.setFollow(follow);
        discuss.setComment(comment);

        for (Comment allgetppermission : list) {
            if(allgetppermission.getFatherId()==0){
                allgetppermission.setComments(findchild(allgetppermission,list));
            }
        }

        Iterator<Comment> iterator = list.iterator();
        while (iterator.hasNext()){
            Comment next = iterator.next();
            if(next.getFatherId()!=0){
                iterator.remove();
            }
        }

        discuss.setComments(list);
        System.out.println(list);
        return R.ok().data("discuss",discuss);
    }

    @Override
    public R getwonderfulcomments(Integer id) {
        //查询讨论点赞数最多的评论
        List<Comment> comments=ljpexclusiveMapper.getwonderfulcomments(id);
        //查询评论的自己评论
//        List<Comment> commentList=ljpexclusiveMapper.getwonderfulcomments1(comments.get(0).getCommentId());
        List<Comment> list=ljpexclusiveMapper.getcommentbytlid(id);

        for (Comment allgetppermission : list) {
            if(allgetppermission.getFatherId()==0){
                allgetppermission.setComments(findchild(allgetppermission,list));
            }
        }

        Iterator<Comment> iterator = list.iterator();
        while (iterator.hasNext()){
            Comment next = iterator.next();
            if(next.getFatherId()!=0){
                iterator.remove();
            }
        }
        int min=0;
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getPraise()>min){
                min=list.get(i).getPraise();
            }
        }
        Comment comment=new Comment();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getPraise()==min){
                comment=list.get(i);
            }
        }

        System.out.println(list);
        return R.ok().data("jingcai",comment);
    }

    @Override
    public R setguanzhu(Integer id, Integer discussid) {
        redisUtil.incr("follow"+discussid,1);
        if(redisUtil.get("follows-"+id)==null){
            List<Follows> follows=new ArrayList<>();
            Follows follows1=new Follows();
            follows1.setUserId(id);
            follows1.setDiscussId(discussid);
            follows.add(follows1);
            redisUtil.set("follows-"+id,follows);
        }else {
            List<Follows> follows=(List<Follows>) redisUtil.get("follows-"+id);
            Follows follows1=new Follows();
            follows1.setUserId(id);
            follows1.setDiscussId(discussid);
            follows.add(follows1);
            redisUtil.set("follows-"+id,follows);

        }
        return R.ok();
    }

    @Override
    public R delguanzhu(Integer id, Integer discussid) {
        redisUtil.decr("follow"+discussid,1);
        List<Follows> follows=(List<Follows>) redisUtil.get("follows-"+id);
        for (int i = 0; i < follows.size(); i++) {
            if (follows.get(i).getDiscussId()==discussid){
                follows.remove(i);
            }
        }
        redisUtil.set("follows-"+id,follows);
        return R.ok();
    }

    @Override
    public R guanzhutype(Integer userid) {
        List<Follows> follows=(List<Follows>) redisUtil.get("follows-"+userid);

        return R.ok().data("follows",follows);
    }

    @Override
    public R setdianzan(Integer userid, Integer commentid) {
        Integer praise=ljpexclusiveMapper.getcommentddbyid(commentid);
        praise++;
        int i=ljpexclusiveMapper.setdianzan(commentid,praise);
        return R.ok();
    }

    @Override
    public R setpl(Integer fid, Integer userid, String contents, Integer id) {
        System.out.println("传来的父id"+fid);
        Comment comment=new Comment();
        comment.setFatherId(fid);
        comment.setUserId(userid);
        comment.setContents(contents);
        comment.setDiscussId(id);
        comment.setIsDel("n");
        LocalDateTime localDateTime=LocalDateTime.now();
        comment.setCreationTime(localDateTime);
        comment.setPraise(0);
        int i=ljpexclusiveMapper.setpl(comment);
        redisUtil.incr("comment"+id,1);

        return R.ok();
    }

    @Override
    public R getindex() {
        List<Discuss> discusses=ljpexclusiveMapper.getdiscussrm();
        LocalDateTime localDateTime=LocalDateTime.now();
        int year = localDateTime.getYear();
        int monthValue = localDateTime.getMonthValue();
        int dayOfMonth = localDateTime.getDayOfMonth();
        String times=year+"-"+monthValue+"-"+dayOfMonth;
        long redeem = redisUtil.getExpire("redeem");
        System.out.println(redeem);
        Map<String,List<CommodityMax>> map1=(Map<String,List<CommodityMax>>)redisUtil.get("redeem");
        List<CommodityMax> list = map1.get(times);
        list.get(0).setTime(redeem);
        ConcurrentMap map=new ConcurrentHashMap();
        map.put("discusses",discusses);
        map.put("xsdh",list);
        return R.ok().data("map",map);
    }

    @Override
    public R gettlbytyle(Integer type) {
        System.out.println("讨论类型"+type);
        List<Discuss> discusses=ljpexclusiveMapper.gettlbytyle(type);

        return R.ok().data("discusses",discusses);
    }

    @Override
    public R adddiscuss(String title, Integer pltype, String contents, Integer userid) {
        Discuss discuss=new Discuss();
        discuss.setTitle(title);
        discuss.setContents(contents);
        discuss.setClassification(pltype);
        discuss.setUserId(userid);
        discuss.setIsDel("n");
        LocalDateTime localDateTime=LocalDateTime.now();
        discuss.setCreationTime(localDateTime);
        int i=ljpexclusiveMapper.adddiscuss(discuss);
        Integer discussId = discuss.getDiscussId();
        redisUtil.set("follow"+discussId,0);
        redisUtil.set("comment"+discussId,0);
        return R.ok();
    }

    @Override
    public R initsignin(Integer userid,String time) {
        System.out.println("时间："+time);
        if(time==""){
            LocalDateTime localDateTime=LocalDateTime.now();
            int year = localDateTime.getYear();
            int monthValue = localDateTime.getMonthValue();
            int dayOfMonth = localDateTime.getDayOfMonth();
            String times=year+"-"+monthValue+"-"+dayOfMonth;
            time=times;
        }
        System.out.println("今日："+redisUtil.get("signin"+userid));
        if (redisUtil.get("signin"+userid)==null){

            return R.ok().message("今日未签到");
        } else{
            ConcurrentHashMap<String,Signin> map=(ConcurrentHashMap<String, Signin>) redisUtil.get("signin"+userid);
            System.out.println(map.get("2022-5-10"));
            if(map.get(time)==null){
                LocalDateTime localDateTime=LocalDateTime.now();
                int year = localDateTime.getYear();
                int monthValue = localDateTime.getMonthValue();
                int dayOfMonth = localDateTime.getDayOfMonth();
                String times=year+"-"+monthValue+"-"+dayOfMonth;
                String[] split = time.split("-");
                String[] split1 = times.split("-");
                if(Integer.valueOf(split[0]) > Integer.valueOf(split1[0])){
                    return R.ok().message("未到时间");
                }else if(Integer.valueOf(split[0]) < Integer.valueOf(split1[0])){
                    return R.ok().message("未签到过期");
                }else {
                    if (Integer.valueOf(split[1]) > Integer.valueOf(split1[1])){
                        return R.ok().message("未到时间");
                    }else if(Integer.valueOf(split[1]) < Integer.valueOf(split1[1])){
                        return R.ok().message("未签到过期");
                    }else {
                        if(Integer.valueOf(split[2]) > Integer.valueOf(split1[2])){
                            return R.ok().message("未到时间");
                        }else if (Integer.valueOf(split[2]) < Integer.valueOf(split1[2])){
                            return R.ok().message("未签到过期");
                        }else {
                            Integer count=(Integer) redisUtil.get("signincount"+userid);
                            return R.ok().message("今日未签到");
                        }
                    }
                }
//                return R.ok().message("未签到");
            }else {

                Integer count=(Integer) redisUtil.get("signincount"+userid);
                return R.ok().message("已签到").data("count",count);
            }
        }

    }

    @Override
    public R setsignin(Integer userid) {
        Signin signin=new Signin();
        signin.setIsin(1);
        LocalDateTime localDateTime=LocalDateTime.now();
        int year = localDateTime.getYear();
        int monthValue = localDateTime.getMonthValue();
        int dayOfMonth = localDateTime.getDayOfMonth();
        String times=year+"-"+monthValue+"-"+dayOfMonth;
        signin.setTime(times);
        if(redisUtil.get("signin"+userid)==null){
            ConcurrentMap<String,Signin> map=new ConcurrentHashMap();
            map.put(times,signin);
            redisUtil.set("signin"+userid,map);
            redisUtil.set("signincount"+userid,1);
        }else {
            ConcurrentMap<String,Signin> map=(ConcurrentMap<String, Signin>) redisUtil.get("signin"+userid);
            map.put(times,signin);
            redisUtil.set("signin"+userid,map);
            redisUtil.incr("signincount"+userid,1);
        }
        Integer count=(Integer) redisUtil.get("signincount"+userid);
        return R.ok().message("已签到").data("count",count);
    }

    @Override
    public R initsc(Integer userid) {
        LocalDateTime localDateTime=LocalDateTime.now();
        int year = localDateTime.getYear();
        int monthValue = localDateTime.getMonthValue();
        int dayOfMonth = localDateTime.getDayOfMonth();
        String times=year+"-"+monthValue+"-"+dayOfMonth;
        long redeem = redisUtil.getExpire("redeem");
        Map<String,List<CommodityMax>> map1=(Map<String,List<CommodityMax>>)redisUtil.get("redeem");
        List<CommodityMax> list1 = map1.get(times);
        list1.get(0).setTime(redeem);
        List<CommodityUnion> commodityUnions = ljpexclusiveMapper.rmcommodity();
        System.out.println("热门："+commodityUnions);
        List<CommodityUnion> cai =new ArrayList<>();
        if (userid !=null){
            List<Commodity> list=ljpexclusiveMapper.inusercai(userid);
            for (Commodity commodity : list) {
                List<CommodityUnion> ll=ljpexclusiveMapper.inusercai1("%"+commodity.getCommodityName()+"%");
                for (CommodityUnion union : ll) {
                    cai.add(union);
                }
            }

        }else {
            cai=ljpexclusiveMapper.unusercai();
        }
        String mar="成功";
//        LocalDateTime localDateTime=LocalDateTime.now();
//        int year = localDateTime.getYear();
//        int monthValue = localDateTime.getMonthValue();
//        int dayOfMonth = localDateTime.getDayOfMonth();
//        String times=year+"-"+monthValue+"-"+dayOfMonth;
        ConcurrentMap concurrentMap=new ConcurrentHashMap();
        concurrentMap.put("remen",commodityUnions);
        concurrentMap.put("caini",cai);
        concurrentMap.put("xsdh",list1);
//        if(redisUtil.get("Limited-time-exchange") !=null){
//            Map<String,List<CommodityUnion>> map=(Map<String, List<CommodityUnion>>) redisUtil.get("Limited-time-exchange");
//            List<CommodityUnion> commodityUnions1 = map.get("times");
//            concurrentMap.put("xians",commodityUnions1);
//        }else {
//            mar="空";
//
//        }

        return R.ok().data("map",concurrentMap).message(mar);
    }

    @Override
    public R initremen() {
        List<CommodityUnion> commodityUnions = ljpexclusiveMapper.rmcommodity1();
        return R.ok().data("remen",commodityUnions);
    }

    @Override
    public R initspxq(Integer commodityid) {
        CommodityMax commodityMax=ljpexclusiveMapper.initspxq(commodityid);
        List<Selection> selections=ljpexclusiveMapper.getsptype(commodityid);
        commodityMax.setSelections(selections);
        List<Commodityimgunion> commodityimgunions=ljpexclusiveMapper.getspimgunion(commodityid);
        List<Commodityimg> list=new ArrayList<>();
        for (Commodityimgunion commodityimgunion : commodityimgunions) {
            list.add(ljpexclusiveMapper.getspimg(commodityimgunion.getCiId()));
        }
        commodityMax.setCommodityimgs(list);
        return R.ok().data("commodity",commodityMax);
    }

    @Override
    public R getguesslike(Integer userid) {
        List<CommodityUnion> cai =new ArrayList<>();
        if (userid !=null){
            List<Commodity> list=ljpexclusiveMapper.inusercai(userid);
            for (Commodity commodity : list) {
                List<CommodityUnion> ll=ljpexclusiveMapper.inusercai1("%"+commodity.getCommodityName()+"%");
                for (CommodityUnion union : ll) {
                    cai.add(union);
                }
            }

        }else {
            cai=ljpexclusiveMapper.unusercai();
        }
        return R.ok().data("cai",cai);
    }

    @Override
    public R addgwc(Integer userid, Integer commodityid, Integer selectionId, Integer sum, Double price) {
        Shoppingcart shoppingcart=new Shoppingcart();
        shoppingcart.setUserId(userid);
        shoppingcart.setCommodityId(commodityid);
        shoppingcart.setSelectionId(selectionId);
        shoppingcart.setSum(sum);
        shoppingcart.setPrice(price);
        shoppingcart.setIsDel("n");
        LocalDateTime localDateTime=LocalDateTime.now();
        shoppingcart.setCreationTime(localDateTime);
        int i=ljpexclusiveMapper.addgwc(shoppingcart);
        return R.ok();
    }

    @Override
    public R addsc(Integer userid, Integer commodityid) {
        Collections collections=new Collections();
        collections.setIsDel("n");
        collections.setUserId(userid);
        collections.setCollectionsId(commodityid);
        LocalDateTime localDateTime=LocalDateTime.now();
        collections.setCreationTime(localDateTime);
        ljpexclusiveMapper.addsc(collections);
//        int i=ljpexclusiveMapper.addgwc(shoppingcart);
        return R.ok();
    }

    @Override
    public R delsc(Integer userid, Integer commodityid) {
        String isdel="y";
        int i=ljpexclusiveMapper.delsc(userid,commodityid,isdel);
        return R.ok();
    }

    @Override
    public R getissc(Integer userid) {
        List<Collections> collections=ljpexclusiveMapper.getissc(userid);
        return R.ok().data("sc",collections);
    }

    @Override
    public R addorder(Integer userid, Integer commodityid, Integer selectionId, Integer sum, Double price) {
        Commodityorder commodityorder=new Commodityorder();

        return null;
    }

    @Override
    public R initmd(Integer commodityid,Integer type) {
        List<Store> stores=ljpexclusiveMapper.initmd(commodityid,type);
        return R.ok().data("stores",stores);
    }

    @Override
    public R temporaryorder(CommodityorderVo commodityorder) {
        String s = UUID.randomUUID().toString();
        Orderuuid orderuuid=new Orderuuid();
        orderuuid.setUuid(s);
        if(redisUtil.get("temporaryorder"+commodityorder.getUserId())==null){
            List<Orderuuid> list=new ArrayList<>();
            list.add(orderuuid);
            redisUtil.set("temporaryorder"+commodityorder.getUserId(),list);
        }else {
            List<Orderuuid> list=(List<Orderuuid>) redisUtil.get("temporaryorder"+commodityorder.getUserId());
            list.add(orderuuid);
            redisUtil.set("temporaryorder"+commodityorder.getUserId(),list);
        }
        redisUtil.set(s,commodityorder,900);
        long expire = redisUtil.getExpire(s);


        return R.ok().data("time",expire);
    }

    @Override
    public R zhifu(CommodityorderVo commodityorder) throws IOException {
        User user=ljpexclusiveMapper.getuserbyuserid(commodityorder.getUserId());
        if(user.getPwPayment().equals(commodityorder.getPassword())){
            commodityorder.setIsDel("n");
            LocalDateTime localDateTime=LocalDateTime.now();
            commodityorder.setCreationTime(localDateTime);
            commodityorder.setCoType(2);
            int i = new Random().nextInt(20) + 1;
            int i1 = new Random().nextInt(9) + 1;
            int i2 = new Random().nextInt(9999) + 1000;
            String s=i+"-"+i1+"-"+i2;
            commodityorder.setTan(s);

            MultipartFile idtest = QrCode.idtest(s);
            String upload = aliOss.upload(idtest);
            commodityorder.setQrcodeimg(upload);
            Commoditystore cs=new Commoditystore();

            if (commodityorder.getDeliveryMethod()==1){
                List<Commoditystore> list=ljpexclusiveMapper.getkc(commodityorder.getSelectionId());
                int max=0;
                for (Commoditystore commoditystore : list) {
                    if(commoditystore.getStock()>max){
                        max=commoditystore.getStock();
                    }
                }

                for (Commoditystore commoditystore : list) {
                    if(commoditystore.getStock()==max){
                        cs=commoditystore;
                    }
                }
                commodityorder.setStoreId(cs.getStoreId());

                if(cs.getStock()>commodityorder.getQuantity()){
                    if(user.getBalance()>commodityorder.getTotal()){
                        ljpexclusiveMapper.updateusermoney(commodityorder.getUserId(),user.getBalance()-commodityorder.getTotal());
                        ljpexclusiveMapper.updatestorestock(cs.getCsId(),cs.getStock()-commodityorder.getQuantity());
                        ljpexclusiveMapper.addcommodityorder(commodityorder);
                        return R.ok();

                    }else {
                        return R.ok().message("余额不足");
                    }
                }else {
                    return R.ok().message("库存不足");
                }
            }else {
                cs= ljpexclusiveMapper.getstorebyid(commodityorder.getStoreId(),commodityorder.getSelectionId());
                if(cs.getStock()>commodityorder.getQuantity()){
                    if(user.getBalance()>commodityorder.getTotal()){
                        ljpexclusiveMapper.updateusermoney(commodityorder.getUserId(),user.getBalance()-commodityorder.getTotal());
                        ljpexclusiveMapper.updatestorestock(cs.getCsId(),cs.getStock()-commodityorder.getQuantity());
                        ljpexclusiveMapper.addcommodityorder(commodityorder);
                        return R.ok().message("成功");

                    }else {
                        return R.ok().message("余额不足");
                    }
                }else {
                    return R.ok().message("库存不足");
                }

            }



        }else {
            return R.ok().message("密码错误");
        }

    }

    @Override
    public R idtest() throws IOException {
//
        return null;
    }

    @Override
    public List<CommodityMax> getcommodityquan() {

        List<CommodityMax> list=ljpexclusiveMapper.getcommodityquan();
        return list;
    }

    @Override
    public List<Commodityimg> getcommodityimg(Integer ciid) {
        List<Commodityimg> list=ljpexclusiveMapper.getcommodityimg(ciid);
        return list;
    }

    @Override
    public List<Commodityimgunion> getcommodityunion(Integer commodityid) {
        List<Commodityimgunion> list=ljpexclusiveMapper.getcommodityunion(commodityid);
        return list;
    }

    @Override
    public List<Selection> getselection() {
        List<Selection> list=ljpexclusiveMapper.getselection();
        return list;
    }

    @Override
    public Integer sunck(Integer commodityId) {
        int sum=ljpexclusiveMapper.sunck(commodityId);
        return sum;
    }

    @Override
    public R initgwc(Integer userid) {
        List<ShoppingcartUnion> list=ljpexclusiveMapper.getshoppingcart(userid);
        for (ShoppingcartUnion shoppingcart : list) {
            shoppingcart.setCommodityUnions(ljpexclusiveMapper.getcommoditybyid(shoppingcart.getCommodityId()));
            System.out.println(shoppingcart.getCommodityUnions());
        }
        for (ShoppingcartUnion shoppingcart : list)
            shoppingcart.setSelections(ljpexclusiveMapper.getselectionbyid(shoppingcart.getSelectionId()));




        return R.ok().data("gwc",list);
    }

    @Override
    public R gwcjs(Integer scid) {
        ShoppingcartUnion shoppingcartUnion=ljpexclusiveMapper.gwcjs(scid);
        shoppingcartUnion.setSelections(ljpexclusiveMapper.getselectionbyid(shoppingcartUnion.getSelectionId()));
        shoppingcartUnion.setCommodityUnions(ljpexclusiveMapper.getcommoditybyid(shoppingcartUnion.getCommodityId()));

        return R.ok().data("su",shoppingcartUnion);
    }

    @Override
    public R delgwc(Integer scid) {
        int i=ljpexclusiveMapper.delgwc(scid);
        return R.ok();
    }

    @Override
    public List<CommodityMax> commoditymaxquan() {
        List<CommodityMax> list=ljpexclusiveMapper.commoditymaxquan();
        return list;
    }

    @Override
    public R initxsdh() {
        LocalDateTime localDateTime=LocalDateTime.now();
        int year = localDateTime.getYear();
        int monthValue = localDateTime.getMonthValue();
        int dayOfMonth = localDateTime.getDayOfMonth();
        String times=year+"-"+monthValue+"-"+dayOfMonth;
        long redeem = redisUtil.getExpire("redeem");
        Map<String,List<CommodityMax>> map1=(Map<String,List<CommodityMax>>)redisUtil.get("redeem");
        List<CommodityMax> list1 = map1.get(times);
        list1.get(0).setTime(redeem);

        return R.ok().data("xsdh",list1);
    }

    @Override
    public R initclassfl() {
        List<Classification> list=ljpexclusiveMapper.initclassfl();
//        for (Classification allgetppermission : list) {
//            if(allgetppermission.getFatherId()==0){
//                allgetppermission.setClassificationList(findchild1(allgetppermission,list));
//            }
//        }
//
//        Iterator<Classification> iterator = list.iterator();
//        while (iterator.hasNext()){
//            Classification next = iterator.next();
//            if(next.getFatherId()!=0){
//                iterator.remove();
//            }
//        }

        return R.ok().data("list",list);
    }

    @Override
    public R getlj(Integer id) {
        List<Classification> classification=ljpexclusiveMapper.getlj(id);
        for (Classification classification1 : classification) {
            List<Classification> list=ljpexclusiveMapper.getzilj(classification1.getClassificationId());
            if(list !=null){
                classification1.setClassificationList(list);
            }
        }

        return R.ok().data("classification",classification);
    }

    @Override
    public R addfootprint(Integer commodityid, Integer userid) {
        Footprint footprint=new Footprint();
        footprint.setCommodityId(commodityid);
        footprint.setUserId(userid);
        footprint.setIsDel("n");
        LocalDateTime localDateTime=LocalDateTime.now();
        footprint.setCreationTime(localDateTime);
        int i=ljpexclusiveMapper.addfootprint(footprint);
        return R.ok();
    }


    private List<Comment> findchild(Comment allgetppermission, List<Comment> allgetppermissions) {
        List<Comment> objects = new ArrayList<>();
        for (Comment permission : allgetppermissions) {
            if(permission.getFatherId()==allgetppermission.getCommentId()){
                objects.add(permission);
                permission.setComments(findchild(permission,allgetppermissions));
            }
        }
        return objects;
    }

    private List<Classification> findchild1(Classification allgetppermission, List<Classification> allgetppermissions) {
        List<Classification> objects = new ArrayList<>();
        for (Classification permission : allgetppermissions) {
            if(permission.getFatherId()==allgetppermission.getFatherId()){
                objects.add(permission);
                permission.setClassificationList(findchild1(permission,allgetppermissions));
            }
        }
        return objects;
    }

}
